package com.mueeee.webspringboot3.utils;

import com.mueeee.webspringboot3.utils.Registry.Registry;
import com.mueeee.webspringboot3.utils.Registry.RegistryKey;
import com.mueeee.webspringboot3.utils.Registry.RegistryValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author chenghui
 * @date 2023/12/20 15:32
 */
public class WinProxyUtils1 {
    private static final Logger log = LoggerFactory.getLogger(WinProxyUtils1.class);
    public static String registryPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
    private static final String PARENT = Registry.HKEY_CURRENT_USER;
    private static final String ProxyOverride = "ProxyOverride";
    private static final String ProxyServer = "ProxyServer";
    private static final String ProxyEnable = "ProxyEnable";

    public static void setProxy(String proxyHost, int proxyPort) {
        try {
            RegistryKey key = Registry.getKey(PARENT + "\\" + WinProxyUtils1.registryPath);
            final RegistryValue proxyOverride = key.getValueByName(ProxyOverride);
            log.info("proxyOverride:{}", proxyOverride);
            String httpProxyExclude = EnvironmentUtils.searchByKey("http.proxy.exclude");
            String httpProxyExcludeString = StringUtils.nvl(httpProxyExclude, ";127.0.0.1;localhost;120.79.74.78");
            if (proxyOverride != null) {
                final String rawValue = proxyOverride.getRawValue();
                if (io.micrometer.common.util.StringUtils.isNotEmpty(rawValue)) {
                    if (!rawValue.contains(httpProxyExcludeString)) {
                        if (!httpProxyExcludeString.startsWith(";")) {
                            httpProxyExcludeString = ";" + httpProxyExcludeString;
                        }
                        final String value = rawValue + httpProxyExcludeString;
                        key.setValue(ProxyOverride, RegistryValue.Type.REG_SZ, '\0', value);
                        log.info("代理设置例外:{}", value);
                    } else {
                        log.info("当前代理例外:{}", proxyOverride);
                    }
                } else {
                    key.setValue(ProxyOverride, RegistryValue.Type.REG_SZ, '\0', httpProxyExcludeString);
                    log.info("代理设置例外:{}", httpProxyExcludeString);
                }

            } else {
                key.setValue(ProxyOverride, RegistryValue.Type.REG_SZ, '\0', ";127.0.0.1;localhost;120.79.74.78");
            }

            final RegistryValue ProxyServerValue = key.getValueByName(ProxyServer);
            log.info("ProxyServer:{}", ProxyServerValue);
            key.setValue(ProxyServer, RegistryValue.Type.REG_SZ, '\0', proxyHost + ":" + proxyPort);

            RegistryValue ProxyEnableValue = key.getValueByName(ProxyEnable);
            log.info("ProxyEnable:{}", ProxyEnableValue);
            key.setValue(ProxyEnable, RegistryValue.Type.REG_DWORD, '\0', "0x1");
        } catch (Exception e) {
            log.error("error:{}", e.getMessage(), e);
        }
    }

    public static void clearProxy() {
        try {
            RegistryKey key = Registry.getKey(PARENT + "\\" + WinProxyUtils1.registryPath);
            key.deleteValue("ProxyServer");
            key.setValue("ProxyEnable", RegistryValue.Type.REG_DWORD, '\0', "0x0");
        } catch (Exception e) {
            log.error("error:{}", e.getMessage(), e);
        }
    }

}
